package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Pack;
import com.lvy.hczn.front.system.domain.business.ProductionProcessingType;
import com.lvy.hczn.front.system.service.IProductionProcessingTypeService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.service.TProductionProcessingTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @projectName: wms-front
 * @package: com.lvy.hczn.transmit.service.impl
 * @className: TProductionProcessingTypeServiceImpl
 * @author: yw
 * @date: 2024/12/31 14:57
 */
@Service
@Slf4j
public class TProductionProcessingTypeServiceImpl implements TProductionProcessingTypeService {

    @Autowired
    private ProducerUtil producerUtil;

    @Autowired
    private IProductionProcessingTypeService productionProcessingTypeService;

    @Override
    public void syncProductionProcessingTypeFromPlatForm(HttpServletRequest request, ParamInfo<ProductionProcessingType> info) {
        if (info.getList() != null && !info.getList().isEmpty()) {
            log.info("syncProductionProcessingTypeFromPlatForm中台同步生产加工方式：{}", JSON.toJSONString(info));
            for (ProductionProcessingType productionProcessingType : info.getList()) {
                if (StrUtil.isEmpty(productionProcessingType.getName())) {
                    throw new UtilException("加工方式为空");
                }
                if (productionProcessingType.getLabourPrice() == null) {
                    throw new UtilException("人工价格为空");
                }
                if (productionProcessingType.getMaterialsPrice() == null) {
                    throw new UtilException("材料价格为空");
                }
                if (productionProcessingType.getType() == null) {
                    throw new UtilException("数据类型为空");
                }
            }

            productionProcessingTypeService.saveOrUpdateBatch(info.getList());

            ParamInfo<ProductionProcessingType> paramInfo = new ParamInfo<>();
            paramInfo.setList(info.getList());
            try {
                producerUtil.sendAsyncMsg(MqConstants.TOPIC_PRODUCTION_PROCESSING_TYPE, null, JSON.toJSONString(paramInfo).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            } catch (Exception e) {
                throw new UtilException("生产加工方式信息转发异常");
            }
        } else {
            throw new UtilException("数据为空");
        }
    }
}
